package com.gurun.camera.account.presenter;

import android.text.TextUtils;

import com.danale.sdk.platform.result.countrycode.CountryCodeResult;
import com.danale.sdk.platform.result.v5.userlogin.UserLoginResult;
import com.danale.sdk.throwable.PlatformApiError;
import com.gurun.camera.R;
import com.gurun.camera.account.activity.AccountUtil;
import com.gurun.camera.account.model.ILoginModel;
import com.gurun.camera.account.model.LoginModelImpl;
import com.gurun.camera.account.model.ObtainCountryModelImpl;
import com.gurun.camera.account.utils.PatternMatchUtil;
import com.gurun.camera.account.view.ILoginView;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by danal on 2016/9/9.
 */

public class LoginPresenterImpl implements ILoginPresenter {
    private ILoginView loginView;
    private ILoginModel loginModel;
    private ObtainCountryModelImpl obtainCountryModel;


    public LoginPresenterImpl(ILoginView view) {
        this.loginView = view;
        loginModel = new LoginModelImpl();
        obtainCountryModel = new ObtainCountryModelImpl();
    }

    /**
     * 检查账户类型 账户为手机时 可选择国别码
     * Check the account type ,account is phone number user can choose country code
     * @param account
     */
    @Override
    public void verifyAccountType(String account) {
        loginView.hideIllegalLayout();
        if (TextUtils.isEmpty(account)) {
            loginView.hideCountry();
            return;
        }

        if (PatternMatchUtil.isNumber(account)) {
            loginView.showCountryCode();
            loginView.showCountryFlag();

        } else {
            loginView.hideCountry();
        }

    }


    /**
     * 登录
     * login
     * @param account 账户
     *                phone number or email
     * @param password 密码
     *                 password
     */

    @Override
    public void login(final String account, final String password) {
        loginView.showloading();

        loginView.showloading();
        loginModel.login(account,password).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<UserLoginResult>() {
                    @Override
                    public void call(UserLoginResult userLoginResult) {
                        loginView.hideloading();
                        loginView.notifyloginResult("SUCCESS");
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        try {
                            loginView.hideloading();
                            if(throwable instanceof PlatformApiError){
                                loginView.notifyloginResult(((PlatformApiError) throwable).getErrorDescription());
                                ((PlatformApiError) throwable).getPlatformErrorCode();
                            } else {
                                throwable.getMessage();
                                loginView.notifyloginResult(AccountUtil.getInstance(null).getAppContext().getResources().getString(R.string.network_error0));
                            }
                        }catch (Exception e){

                        }
                    }
                });

    }

    /**
     * 获取当前国家国别码  Get the current country code
     */

    @Override
    public void getCurrentCountryCode() {
        obtainCountryModel.obtainCountryCodelist().observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CountryCodeResult>() {
                    @Override
                    public void call(CountryCodeResult countryCodeResult) {
                        for (int i = 0; i < countryCodeResult.getCountryCodes().size(); i++) {
                            if (countryCodeResult.getCountryCodes().get(i).isDefault() && loginView != null) {
                                loginView.noyifyCurrentCountryCode(countryCodeResult.getCountryCodes().get(i));
                            }
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {


                    }
                });

    }

}
